              /*
nt.js: provides the following functionality for the main component of the toolbar - get current page url, opens new windows, 
toggles between friends and network view, changes the progress bar
author: Alla Genkina, 2004-2005
*/


var page;
var webpage;
var num;
var req;

//listens for the loading of a new webpage and calls a function to get the url
document.addEventListener("load", function() { geturl(); }, true);

//gets current url from firefox's address textbox, used the DOM Inspector to get texbox ID
function geturl () {
	
	var urlbox = document.getElementById("urlbar");
	var url_val = urlbox.value;
	
	if(webpage == null){
		webpage = url_val;
		set_ui(url_val);
	}
	else if(webpage != url_val) {
		webpage = url_val;
		set_ui(url_val);
	}
}

//open window to log in
function login() {
	var l = "http://silo.cs.indiana.edu:13795/login.html";
	window.open(l, "Login", "chrome,width=250,height=300,left=362, top=184");	
}

//open window to add friends
function add_nym() {
	var an ="http://silo.cs.indiana.edu:13795/add_nym.html";
	window.open(an, "Add New Username", "chrome,width=250,height=300,left=362, top=184");	
}

//open window to rate site 
function rate_down() {
	
	var new_page = webpage.split('/');
	var d = "http://silo.cs.indiana.edu:13795/rate_down.html?nym=home&url=" + new_page[2];
	window.open(d, "Rate Site", "chrome,width=350,height=300,left=362, top=184");		
}

//open window to rate site 
function rate_up() {
	var new_page = webpage.split('/');
	var u = "http://silo.cs.indiana.edu:13795/rate_up.html?nym=home&url=" + new_page[2];
	window.open(u, "Rate Site", "chrome,width=350,height=300,left=362, top=184, scrollbars=no");	
}

//open window to add friends
function friend_request() {
	window.open("chrome://nt/content/friend_request.xul", "Read Comments", "chrome,width=400,height=275,left=362, top=184");	
}

//open window to add friends
function add_friend() {
	var af = "http://silo.cs.indiana.edu:13795/add_friend.html?nym=home";
	window.open(af, "Read Comments", "chrome,width=400,height=275,left=362, top=184");	
}

//open windwo to delete friends
function del_friend() {
	var df = "http://silo.cs.indiana.edu:13795/del_friend.html?nym=home";
	window.open(df, "Read Comments", "chrome,width=400,height=275,left=362, top=184");	
}

//open window to read comments
function comments() {
	var c = "http://silo.cs.indiana.edu:13795/comments.html?nym=home";
	window.open(c, "Read Comments", "chrome,width=400,height=275,left=362, top=184");	
}

//open window to add broadcasters
function add_broad() {
	var ab = "http://silo.cs.indiana.edu:13795/add_broad.html?nym=home";
	window.open(ab, "Read Comments", "chrome,width=400,height=275,left=362, top=184");	
}

//open window to delete broadcasters
function del_broad() {
	var db = "http://silo.cs.indiana.edu:13795/del_broad.html?nym=home";
	window.open(db, "Read Comments", "chrome,width=400,height=275,left=362, top=184");	
}


//start of ajax to populate the toolbar

//callback processes xml responce from server
function callback()
{
	if(req.readyState == 4)
	{
		if(req.status == 200) {
			if(req.responseXML != null) {
				//alert(req.responseText);
				var items = [];	
				var bname = new Array();
				var brating = new Array();
				var brate1;
				var nl = req.responseXML.getElementsByTagName('positiverating');
				var n2 = req.responseXML.getElementsByTagName('negativerating');
				var n3 = req.responseXML.getElementsByTagName('numberofcomments');
				var n4 = req.responseXML.getElementsByTagName('numberofbuddies');
				var b1 = req.responseXML.getElementsByTagName('Broadcaster');	
						
																		
				for(var i=0; i< b1.length; i++) {
					
					var tmpname = b1.item(i).getElementsByTagName('name');
					var tmprate = b1.item(i).getElementsByTagName('rating');
				 	bname[i] = tmpname.item(0).firstChild.nodeValue;
				 	brating[i] = tmprate.item(0).firstChild.nodeValue;					
				}
				
				var textValue1 = nl.item(0).firstChild.nodeValue; //positiverating
				textValue1 = parseInt(textValue1);
				textValue1 = textValue1.toString();
				var textValue2 = n2.item(0).firstChild.nodeValue; //negativerating
				var textValue2 = parseInt(textValue2);
				textValue2 = textValue2.toString(); 
				var textValue3 = n3.item(0).firstChild.nodeValue; //number of comments
				var textValue4 = n4.item(0).firstChild.nodeValue; //number of buddies
				
				update_gui(textValue1, textValue2, textValue3, textValue4, bname, brating);				
			}
		}
		else
		{
			//alert(req.status);
		}
    }
    else
    {
		//alert(req.readyState);
	}
}

	
//loadXMLDoc sends data to server
function loadXMLDoc(x) {
	var url_val = x;
	
    if(window.XMLHttpRequest)
    {
       try
       {
         req = new XMLHttpRequest();
		// alert ("Creating new XMLHttpRequest");
       }
       catch(e)
       {
         req = false;
       }
    }
    else if(window.ActiveXObject)
    {
       try
       {
           req = new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(e)
       {
               try
               {
                       req = new ActiveXObject("Microsoft.XMLHTTP");
               }
               catch(e)
               {
                       req = false;
               }
       }
    }
    if(req)
    {
	   req.onreadystatechange = callback;
       req.open("GET", url_val, true);
       req.send("");
    }
}

//creates the initial toolbar UI based on the url
function set_ui(w) {
	
	var url = w;
	
	var temp = new Array();
	temp = url.split('/'); //will give www.domain.com
	url = "http://silo.cs.indiana.edu:13795/servlet/NetTrustFinalServlet?url_val="+temp[2];
	loadXMLDoc(url);
}

//takes the processed info from server and displays on toolbar
function update_gui(positive, negative, comments, friends, bname, brating){
	
	var p_rating = positive;
	var n_rating = negative;
	var num_comments = comments;
	var num_friends = friends;
	
	var red1 = document.getElementById("red1");
	var red2 = document.getElementById("red2");
	var red3 = document.getElementById("red3");
	var red4 = document.getElementById("red4");
	var red5 = document.getElementById("red5");
	
	var green1 = document.getElementById("green1");
	var green2 = document.getElementById("green2");
	var green3 = document.getElementById("green3");
	var green4 = document.getElementById("green4");
	var green5 = document.getElementById("green5");
	
	
	//set positive ratings on gui
	switch(p_rating){
	
	case '0':
		green1.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green2.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green3.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green4.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;
	
	case '1':
		green1.setAttribute("src","chrome://nt/content/skin/green.gif");
		green2.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green3.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green4.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;
	
	case '2':
		green1.setAttribute("src","chrome://nt/content/skin/green.gif");
		green2.setAttribute("src","chrome://nt/content/skin/green.gif");
		green3.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green4.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;	
	
	case '3':
		green1.setAttribute("src","chrome://nt/content/skin/green.gif");
		green2.setAttribute("src","chrome://nt/content/skin/green.gif");
		green3.setAttribute("src","chrome://nt/content/skin/green.gif");
		green4.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		green5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;
	
	case '4':
		green1.setAttribute("src","chrome://nt/content/skin/green.gif");
		green2.setAttribute("src","chrome://nt/content/skin/green.gif");
		green3.setAttribute("src","chrome://nt/content/skin/green.gif");
		green4.setAttribute("src","chrome://nt/content/skin/green.gif");
		green5.setAttribute("src","chrome://nt/content/skin/green.gif");
	break;
	
	case '5':
		green1.setAttribute("src","chrome://nt/content/skin/green.gif");
		green2.setAttribute("src","chrome://nt/content/skin/green.gif");
		green3.setAttribute("src","chrome://nt/content/skin/green.gif");
		green4.setAttribute("src","chrome://nt/content/skin/green.gif");
		green5.setAttribute("src","chrome://nt/content/skin/green.gif");
	break;	
	
	case '10':
		alert("Warning: This website does not appear to be valid.");
	break;
	}
	
	//set negative ratings on gui
	switch(n_rating) {
	
	case '0':
		red1.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red2.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red3.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red4.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;
	
	case '-1':
		
		red1.setAttribute("src","chrome://nt/content/skin/red.gif");
		red2.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red3.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red4.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;
	
	case '-2':
	
		red1.setAttribute("src","chrome://nt/content/skin/red.gif");
		red2.setAttribute("src","chrome://nt/content/skin/red.gif");
		red3.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red4.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;
	
	case '-3':
		red1.setAttribute("src","chrome://nt/content/skin/red.gif");
		red2.setAttribute("src","chrome://nt/content/skin/red.gif");
		red3.setAttribute("src","chrome://nt/content/skin/red.gif");
		red4.setAttribute("src","chrome://nt/content/skin/neutral.gif");
		red5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;
	
	case '-4':
		red1.setAttribute("src","chrome://nt/content/skin/red.gif");
		red2.setAttribute("src","chrome://nt/content/skin/red.gif");
		red3.setAttribute("src","chrome://nt/content/skin/red.gif");
		red4.setAttribute("src","chrome://nt/content/skin/red.gif");
		red5.setAttribute("src","chrome://nt/content/skin/neutral.gif");
	break;
	
	case '-5':
		red1.setAttribute("src","chrome://nt/content/skin/red.gif");
		red2.setAttribute("src","chrome://nt/content/skin/red.gif");
		red3.setAttribute("src","chrome://nt/content/skin/red.gif");
		red4.setAttribute("src","chrome://nt/content/skin/red.gif");
		red5.setAttribute("src","chrome://nt/content/skin/red.gif");
	break;
	
	case '-10':
		alert("Warning: This website does not appear to be valid.");
	break;
	}
	
	//set number friends
	num_friends = format(num_friends);
	var friends = document.getElementById("lfriends");
	friends.setAttribute("value", "Friends: "+ num_friends);
	
	//set number of comments
	num_comments = format(num_comments);
	var comments = document.getElementById("lcomments");
	comments.setAttribute("value", "Comments: "+ num_comments);
	
	//set broadcasters
	
	var bsmile = new Array(5);
	var blabel = new Array (5);
	var bsep = new Array(4);
	
	bsmile[0] = document.getElementById("mb1");
	blabel[0] = document.getElementById("lb1");
	bsep[0] = document.getElementById("tb1");
	bsmile[1] = document.getElementById("mb2");
	blabel[1] = document.getElementById("lb2");
	bsep[1] = document.getElementById("tb2");
	bsmile[2] = document.getElementById("mb3");
	blabel[2] = document.getElementById("lb3");
	bsep[2] = document.getElementById("tb3");
	bsmile[3] = document.getElementById("mb4");
	blabel[3] = document.getElementById("lb4");
	bsep[3] = document.getElementById("tb4");
	bsmile[4] = document.getElementById("mb5");
	blabel[4] = document.getElementById("lb5");	
	
	for(var k=0; k < bname.length; k++){
		
		if(brating[k]== 1){
			bsmile[k].setAttribute("src","chrome://nt/content/skin/esmile.png");
			bsmile[k].setAttribute("hidden","false");  	
		}
		else {
			bsmile[k].setAttribute("src","chrome://nt/content/skin/esad.png"); 
			bsmile[k].setAttribute("hidden","false");  	
		}
		
		blabel[k].setAttribute("value", format(bname[k]));
		blabel[k].setAttribute("hidden","false");  
		bsep[k].setAttribute("hidden","false"); 
		
	}
		
		
}

function format(val) {
	
	vlength = val.length;
	val = val.slice(1, vlength-1);	
	return val;
		
}